package log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class LogObras extends Log {
	
	private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");

	//protected static String path = "C:/workspace/IstarJade_Home/file/Obras/";

	protected static String path = "D:\\Documents and Settings\\csid\\workspace\\IstarJade_Work\\file\\";
	
	//protected static String path = "U:/myworkspace/IstarJade_20072010/file/Obras/";
	//protected static String path = "U:\\LattesScholar\\";
	
	private static final String executionTime = dateFormat.format(new Date());
	private BufferedWriter out = null;
	private HashMap<String, File> logFiles = new HashMap<String, File>(); 
	
	// The singleton instance of this LOG
	private static LogObras theInstance;
	
	// This is the method to access the singleton log object
	public static LogObras getInstance(String _pathToLogFile){
		theInstance = new LogObras(_pathToLogFile);
		return theInstance;
	}
	
	
	// Private constructor
	protected LogObras(String _pathToLogFile){
		super(_pathToLogFile);
	}
	


	
	public void log(String agentName, String _phrase, String name)  {
		File output=null;
		String file_name =  path + agentName + name;
		output = logFiles.get(file_name);
		try {
			if (output==null){
				output = new File(file_name);
				output.createNewFile();
				logFiles.put(file_name, output);
			}

			FileWriter fw = new FileWriter(output, true);
			BufferedWriter out = new BufferedWriter(fw);
			out.write(_phrase);
			out.newLine();
			out.flush();
			out.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} 
		
	}
	
	public void renameLog(String oldName, String newName){
		File old = new File (path + oldName);
		File newFile = new File (path + newName);
		old.renameTo(newFile);
	}
	
}
